#ifndef DOWNLOADTHREAD_H
#define DOWNLOADTHREAD_H

#include <QObject>
#include <QNetworkReply>

class QFile;
class QNetworkAccessManager;

class DownloadThread : public QObject
{
    Q_OBJECT
public:
    enum DownloadState{ D_Waiting, D_Downloading, D_Pause,
                        D_Stop, D_Finished };

    explicit DownloadThread(QObject *parent = 0);
    void startDownload(int index, const QString& url, QFile *file,
                       qint64 startPoint, qint64 endPoint,
                       qint64 readySize = 0);
    qint64 getReadySize() const { return m_readySize;}

signals:
    void finished(int index);
    void errorCode(int index, QString errorString);
    void progressChange();

public slots:
    void stop();
    void restart();

private slots:
    void finishedSlot();
    void readyReadSlot();
    void errorSlot(QNetworkReply::NetworkError code);

private:
    int m_index;
    QString m_url;
    qint64 m_startPoint;
    qint64 m_endPoint;
    qint64 m_readySize;
    QFile *m_file;
    QNetworkAccessManager *m_manager;
    QNetworkReply *m_reply;
    DownloadState m_state;

};

#endif // DOWNLOADTHREAD_H
